//
//  TodayViewController.m
//  ICup
//
//  Created by BuJi on 14/11/25.
//  Copyright (c) 2014年 Echo. All rights reserved.
//

#import "TodayViewController.h"
#import "RecInfo.h"

@interface TodayViewController ()

@end

@implementation TodayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = RGB(56, 63, 73, 1);
    
    NSMutableArray *_sectionIndexTitles = [[NSMutableArray alloc] init];
    NSMutableArray *curArray = [[NSMutableArray alloc] init];
    NSString *path = [RecInfo storePathWithFileName:ICUPRECINFO];
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        NSArray *array =  [RecInfo unarchiverObjectFromPath:path key:ICUPRECINFO];
        [curArray addObjectsFromArray:array];
    }
    
    NSDateFormatter *formatter	=  [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyMMdd"];//HH:mm
    NSString  *strdate = [formatter stringFromDate:[NSDate date]];
    NSLog(@"strdate %@", strdate);
    
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    for (NSMutableArray *array in curArray) {
        if (array.count > 4) {
            NSNumber *ml = [array objectAtIndex:0];
            NSNumber *year = [array objectAtIndex:1];
            NSNumber *month = [array objectAtIndex:2];
            NSNumber *day = [array objectAtIndex:3];
            NSString *msg = [NSString stringWithFormat:@"%@-%@", month, day];
            
            NSString *msg33 = [NSString stringWithFormat:@"%02d%02d%02d", [year integerValue], [month integerValue], [day integerValue]];
            
            if ([strdate integerValue] >= [msg33 integerValue]) {
                if ([dic objectForKey:msg]) {
                    NSNumber *preMl = [dic objectForKey:msg];
                    ml = [NSNumber numberWithInteger:[preMl integerValue]+[ml integerValue]];
                }
                [dic setObject:ml forKey:msg];
            }
        }
    }
    
    NSArray *nsArry = [[dic allKeys] sortedArrayUsingSelector:@selector(compare:)];
    for (NSString *time in nsArry) {
        [_sectionIndexTitles addObject:time];
    }
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.backgroundColor = [UIColor clearColor];
    scrollView.frame = CGRectMake(0, 120, SCREEN_WIDTH, 200);
    [self.view addSubview:scrollView];
    
    
    NSInteger maxCapacity = 100;
    
    
    for (NSString *string in  [dic allValues]) {
        if ([string integerValue] > maxCapacity) {
            maxCapacity = [string integerValue];
        }
    }
    
    [formatter setDateFormat:@"yyyy-MM"];//HH:mm
    strdate = [formatter stringFromDate:[NSDate date]];
    
    self.title = strdate;
    
    [formatter setDateFormat:@"yy-MM-dd"];//HH:mm
    strdate = [formatter stringFromDate:[NSDate date]];
    
    for (int i=0; i<_sectionIndexTitles.count; i++) {
        
        UIView *h_view = [[UIView alloc] initWithFrame:CGRectMake(10+i*50, 0, 40, 150)];
        h_view.layer.masksToBounds = YES;
        h_view.layer.cornerRadius = 5;
        h_view.backgroundColor = RGB(75, 82, 92, 1);
        [scrollView addSubview:h_view];
        
        NSString *value = [dic objectForKey:[_sectionIndexTitles objectAtIndex:i]];
        
        NSInteger curCapacity = [value integerValue];
        
        NSInteger height = 140*curCapacity/maxCapacity;
        
        UIView *sub_view = [[UIView alloc] initWithFrame:CGRectMake(0, 150-height, 40, height)];
        sub_view.layer.masksToBounds = YES;
        sub_view.layer.cornerRadius = 5;
        sub_view.backgroundColor = RGB(83, 230, 255, 1);
        [h_view addSubview:sub_view];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(FX(h_view), FY(h_view)+FH(h_view), FW(h_view), 20)];
        label.textAlignment = UITextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        label.text = [_sectionIndexTitles objectAtIndex:i];
        if ([strdate isEqualToString:[_sectionIndexTitles objectAtIndex:i]]) {
            label.text = @"今天";
        }
        label.font = [UIFont systemFontOfSize:12];
        label.backgroundColor = [UIColor clearColor];
        [scrollView addSubview:label];
    }
    
    scrollView.contentSize = CGSizeMake(_sectionIndexTitles.count*50, FH(scrollView));
    
    
    UIButton *AdView = [[UIButton alloc] initWithFrame:RECT((SCREEN_WIDTH-120)/2, FY(scrollView)+FH(scrollView), 120, 44)];
    [AdView setBackgroundImage:IMG(@"今天 底色 改.png") forState:UIControlStateNormal];
    [AdView setTitle:@"今天" forState:UIControlStateNormal];
    [AdView addTarget:self action:@selector(AdViewPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:AdView];
    
}
-(void)AdViewPressed{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 57;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  0;
}

- (UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *CellIdentifier = @"SettingBaseCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.backgroundColor = [UIColor clearColor];//ios7必须要这样处理，不然默认会是白色
    return cell;
}
@end
